//
//  DYSignatureViewController.m
//  DaYing
//
//  Created by 黄轩 on 2024/1/13.
//  Copyright © 2024 DaYing. All rights reserved.
//

#import "DYSignatureViewController.h"

#import "DYLotteryDecisionImageCell.h"
#import "DYLotteryDecisionButtonCell.h"

#import "DYSignatureResultViewController.h"

@interface DYSignatureViewController ()

@end

@implementation DYSignatureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setNavigationItemTitleViewWithTitle:@"抽签做决定"];

    [self.tableView registerNib:[UINib nibWithNibName:@"DYLotteryDecisionImageCell" bundle:nil] forCellReuseIdentifier:@"DYLotteryDecisionImageCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"DYLotteryDecisionButtonCell" bundle:nil] forCellReuseIdentifier:@"DYLotteryDecisionButtonCell"];
    
    self.tableView.frame = CGRectMake(0,self.navigationBarHeight, kScreenWidth, kScreenHeight - self.navigationBarHeight);
    [self.view bringSubviewToFront:self.navigationBar];
}

//成功刷新界面
- (void)subscriptionSuccessfullyRefreshed {
    [self refreshData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self refreshData];
}

- (void)complete {
    [self refreshData];
}

#pragma mark - cellDataSource

- (NSArray *)cellDataSource {
    
    if (!self.dataSource) {
        NSMutableArray *arr = [NSMutableArray arrayWithCapacity:4];
        
        __block NSMutableArray *subarr = nil;
        __block NSDictionary *dict = nil;
        
        subarr = [NSMutableArray arrayWithCapacity:1];

        dict =  @{@"class":DYLotteryDecisionImageCell.class,
                  @"height":@([DYLotteryDecisionImageCell getCellFrame:[NSNumber numberWithFloat:500]]),
                  @"delegate":@YES};
        [subarr addObject:dict];

        dict =  @{@"class":DYLotteryDecisionButtonCell.class,
                  @"height":@([DYLotteryDecisionButtonCell getCellFrame:[NSNumber numberWithFloat:100]]),
                  @"data":[DYLotteryDecisionButtonCellModel ittemModelWithTitle:@"立即抽签"],
                  @"delegate":@YES};
        [subarr addObject:dict];
        
        [arr addObject:subarr];

        self.dataSource = arr;
    }
    return self.dataSource;
}

#pragma mark - QBlotteryDecisionButtonCellDelegate 抽签结果

- (void)lotteryDecisionButtonCell:(DYLotteryDecisionButtonCell *)cell button:(UIButton *)sender {
    DYLotteryDecisionImageCell *lotteryDecisionImageCell = [self.tableView viewWithTag:10000];
    [lotteryDecisionImageCell start];
    [self performSelector:@selector(delayedAction) withObject:nil afterDelay:3.0];
}

- (void)delayedAction {
    DYLotteryDecisionImageCell *lotteryDecisionImageCell = [self.tableView viewWithTag:10000];
    [lotteryDecisionImageCell end];
    //延时3秒后执行的代码
    [self gotoSignatureResultViewController];
}

#pragma mark - goto

//抽签结果
- (void)gotoSignatureResultViewController {
    NSMutableArray *ary = [NSMutableArray arrayWithCapacity:10];
    [_signatureList enumerateObjectsUsingBlock:^(NSString *str, NSUInteger idx, BOOL * _Nonnull stop) {
        if (str.length > 0) {
            [ary addObject:str];
        }
    }];
    
    int i = arc4random() % ary.count;
    
    DYSignatureResultViewController *vc = [DYSignatureResultViewController new];
    vc.signatureTitle = _signatureTitle;
    vc.signatureList = ary;
    vc.index = i;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
